# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/8 22:21
# @Author   : Robot-Zsj
"""
description:

input
7 -2 6 6 9
6 7 -2 9 6
output
-2 0 20 0 6
"""


class TreeNode:
    def __init__(self, num):
        self.num = num
        self.childSum = 0
        self.leftChild = None
        self.rightChild = None


midOrder = list(map(int, input().split()))
preOrder = list(map(int, input().split()))

n = len(midOrder)

midIndexMap = {}

for j in range(n):
    num = midOrder[j]
    midIndexMap.setdefault(num, [])
    midIndexMap[num].append(j)


def notEquals(midL, preL, size):
    arr1 = sorted(midOrder[midL:midL + size])
    arr2 = sorted(preOrder[preL:preL + size])

    for i in range(size):
        if arr1[i] != arr2[i]:
            return True
    return False


def buildTree(midL, midR, preL, preR):
    if preL > preR:
        return None

    rootNum = preOrder[preL]
    root = TreeNode(rootNum)

    for idx in midIndexMap[rootNum]:
        if idx < midL or idx > midR:
            continue

        leftLen = idx - midL
        if notEquals(midL, preL + 1, leftLen):
            continue

        rightLen = midR - idx
        if notEquals(idx + 1, preR - rightLen + 1, rightLen):
            continue

        root.leftChild = buildTree(midL, idx - 1, preL + 1, preL + leftLen)
        root.rightChild = buildTree(idx + 1, midR, preR - rightLen + 1, preR)

        leftChildSum = 0 if root.leftChild is None else (
                root.leftChild.num + root.leftChild.childSum
        )
        rightChildSum = 0 if root.rightChild is None else (
                root.rightChild.num + root.rightChild.childSum
        )

        root.childSum = leftChildSum + rightChildSum
        break
    return root


def getMidOrder(root, res):
    if root is None:
        return

    getMidOrder(root.leftChild, res)
    res.append(root.childSum)
    getMidOrder(root.rightChild, res)


def getResult():
    root = buildTree(0, n - 1, 0, n - 1)

    res = []
    getMidOrder(root, res)
    return " ".join(map(str, res))


print(getResult())
